# example.py
#
# Remove duplicate entries from a sequence while keeping order

def dedupe(items):
    seen = set()
    # 对元素进行遍历加入set里面
    for item in items:
        # 当item是新元素的时候，才进行加入
        if item not in seen:
            yield item
            seen.add(item)


def dedupe_dict(dict_, key=None):
    seen = set()
    for item in dict_:
        val = item if key is None else key(item)
        if val not in seen:
            yield item
            seen.add(val)


if __name__ == '__main__':
    a = [1, 5, 2, 1, 9, 1, 5, 10]
    print(a)
    # 也能去除重复元素，但是会更改元素在原来序列中的位置
    print(set(a))
    print(list(dedupe(a)))
    print("="*20)

    a = [
        {'x': 2, 'y': 3},
        {'x': 1, 'y': 4},
        {'x': 2, 'y': 3},
        {'x': 2, 'y': 3},
        {'x': 10, 'y': 15}
    ]
    print(a)
    print(list(dedupe_dict(a, key=lambda a: (a['x'], a['y']))))
